package adventure.placeable.trigger.conditions;

import java.util.Random;

import player.Player;
import player.PlayerEdit;
import adventure.room.RoomManager;

/**
 *
 * @author Michael Hanns
 *
 */
public class PlayerCondition extends TriggerCondition implements Cloneable {

    private String strval;
    private int value;
    private int quantity;

    public PlayerCondition(String condition, int value, int quant, String strval, boolean not) {
        super(condition, "PLAYER", not);
        this.value = value;
        this.quantity = quant;
        this.strval = strval;
    }

    public String getStringVal() {
        return strval;
    }

    public int getIntVal() {
        return value;
    }

    public int getQuantity() {
        return quantity;
    }

    @Override
    public String getDescription() {
        String notTxt;
        if(not()) {
            notTxt = "NOT ";
        } else {
            notTxt = "";
        }
        String description = notTxt + dependant + " " + condition + " ";

        if (strval.length() > 0) {
            description = description + strval + " ";
        }
        if (hasValue()) {
            description = description + "Value: " + value + " ";
        }
        if (hasQuantity()) {
            description = description + "Quantity: " + quantity + " ";
        }

        return description;
    }

    private boolean hasValue() {
        if (condition.equals("PREVIOUSFAILED")) {
            return false;
        } else if (condition.equalsIgnoreCase("HASCURRENCYOVER")) {
            return false;
        } else if (condition.equalsIgnoreCase("HASCURRENCYUNDER")) {
            return false;
        } else if (condition.equalsIgnoreCase("HASCURRENCYATLEAST")) {
            return false;
        } else if (condition.equalsIgnoreCase("HASCURRENCYATMOST")) {
            return false;
        }
        return true;
    }

    private boolean hasQuantity() {
        if (condition.equalsIgnoreCase("HASITEM")) {
            return true;
        } else if (condition.equalsIgnoreCase("HASCURRENCYOVER")) {
            return true;
        } else if (condition.equalsIgnoreCase("HASCURRENCYUNDER")) {
            return true;
        } else if (condition.equalsIgnoreCase("HASCURRENCYATLEAST")) {
            return true;
        } else if (condition.equalsIgnoreCase("HASCURRENCYATMOST")) {
            return true;
        } else if (condition.equalsIgnoreCase("NAMEEQUALS")) {
            return true;
        }
        return false;
    }

    @Override
    public boolean meetsCondition(RoomManager r, Player p, String i, boolean previousFailed) {
        boolean success = false;
        if (condition.equals("PREVIOUSFAILED")) {
            success =  previousFailed;
        } else if (condition.equalsIgnoreCase("HASITEM")) {
            success =  hasItem(p);
        } else if (condition.equalsIgnoreCase("HASCURRENCYOVER")) {
            success =  hasOverCurrency(p);
        } else if (condition.equalsIgnoreCase("HASCURRENCYATLEAST")) {
            success =  hasAtLeastCurrency(p);
        } else if (condition.equalsIgnoreCase("HASCURRENCYUNDER")) {
            success =  hasUnderCurrency(p);
        } else if (condition.equalsIgnoreCase("HASCURRENCYATMOST")) {
            success =  hasAtMostCurrency(p);
        } else if (condition.equalsIgnoreCase("HASHEALTH")) {
            success =  hasHealth(p);
        } else if (condition.equalsIgnoreCase("HASSPECIAL")) {
            success =  hasSpecial(p);
        } else if (condition.equalsIgnoreCase("HASATTACK")) {
            success =  hasAttack(p);
        } else if (condition.equalsIgnoreCase("HASDEFENCE")) {
            success =  hasDefence(p);
        } else if (condition.equalsIgnoreCase("HASINTEL")) {
            success =  hasIntelligence(p);
        } else if (condition.equalsIgnoreCase("HASSPEED")) {
            success =  hasSpeed(p);
        } else if (condition.equalsIgnoreCase("HASLUCK")) {
            success =  hasLuck(p);
        } else if (condition.length() > 9 && condition.toUpperCase().startsWith("STATCHECK")) {
            success = statCheck(p, condition.substring(9));
        } else if (condition.equalsIgnoreCase("ISVOCATION")) {
            success =  isVocation(p);
        } else if (condition.equalsIgnoreCase("KNOWSSKILL")) {
            success =  knowsSkill(p);
        } else if (condition.equalsIgnoreCase("ISLEVEL")) {
            success =  isLevel(p);
        } else if (condition.equalsIgnoreCase("HASLOGENTRY")) {
            success =  hasLogEntry(p);
        } else if (condition.equalsIgnoreCase("HASNOTLOGENTRY")) {
            success =  !hasLogEntry(p);
        } else if (condition.equalsIgnoreCase("COMPLETEDLOGENTRY")) {
            success =  completedLogEntry(p);
        } else if (condition.equalsIgnoreCase("INCOMPLETELOGENTRY")) {
            success =  hasIncompleteEntry(p);
        } else if (condition.equalsIgnoreCase("NAMEEQUALS")) {
            success =  nameEquals(p);
        } else {
            return false;
        }
        
        if(not()) {
            success = !success;
        }
        
        return success;
    }

    @Override
    public boolean isUserSpecified() {
        return false;
    }

    private boolean hasItem(Player p) {
        if (p.hasItem(value, quantity)) {
            return true;
        }
        return false;
    }

    private boolean isVocation(Player p) {
        if (p.isVocation(value)) {
            return true;
        }
        return false;
    }

    private boolean knowsSkill(Player p) {
        if (p.knowsSkill(value)) {
            System.out.println("Player knows skill!");
            return true;
        }
        System.out.println("Player does not know skill!");
        return false;
    }

    private boolean hasOverCurrency(Player p) {
        return p.getCurrencyAmount() > quantity;
    }

    private boolean hasAtLeastCurrency(Player p) {
        return p.getCurrencyAmount() >= quantity;
    }

    private boolean hasUnderCurrency(Player p) {
        return p.getCurrencyAmount() < quantity;
    }

    private boolean hasAtMostCurrency(Player p) {
        return p.getCurrencyAmount() <= quantity;
    }

    private boolean hasHealth(Player p) {
        if (((PlayerEdit) p).getHealth() >= value) {
            return true;
        }
        return false;
    }

    private boolean hasSpecial(Player p) {
        if (((PlayerEdit) p).getSpecial() >= value) {
            return true;
        }
        return false;
    }

    private boolean hasAttack(Player p) {
        if (p.getAttack() >= value) {
            return true;
        }
        return false;
    }

    private boolean hasDefence(Player p) {
        if (p.getDefence() >= value) {
            return true;
        }
        return false;
    }

    private boolean hasIntelligence(Player p) {
        if (p.getIntelligence() >= value) {
            return true;
        }
        return false;
    }

    private boolean hasSpeed(Player p) {
        if (p.getSpeed() >= value) {
            return true;
        }
        return false;
    }

    private boolean hasLuck(Player p) {
        if (p.getLuck() >= value) {
            return true;
        }
        return false;
    }

    private boolean statCheck(Player p, String stat) {
        Random r = new Random();
        int rVal = r.nextInt(value) + 1;

        if (stat.equalsIgnoreCase("ATTACK")) {
            if (p.getAttack() > rVal) {
                return true;
            }
        } else if (stat.equalsIgnoreCase("DEFENCE")) {
            if (p.getDefence() > rVal) {
                return true;
            }
        } else if (stat.equalsIgnoreCase("INTEL")) {
            if (p.getIntelligence() > rVal) {
                return true;
            }
        } else if (stat.equalsIgnoreCase("SPEED")) {
            if (p.getSpeed() > rVal) {
                return true;
            }
        } else if (stat.equalsIgnoreCase("LUCK")) {
            if (p.getLuck() > rVal) {
                return true;
            }
        }
        return false;
    }

    private boolean isLevel(Player p) {
        if (p.getLevel() >= value) {
            return true;
        }
        return false;
    }

    private boolean hasLogEntry(Player p) {
        if (p.hasLogEntry(value)) {
            return true;
        }
        return false;
    }

    private boolean hasIncompleteEntry(Player p) {
        if (p.hasIncompleteLogEntry(value)) {
            return true;
        }
        return false;
    }

    private boolean completedLogEntry(Player p) {
        if (p.completedLogEntry(value)) {
            return true;
        }
        return false;
    }

    private boolean nameEquals(Player p) {
        if (p.getName().equalsIgnoreCase(strval)) {
            return true;
        }
        return false;
    }
}
